<template>
  <div id="charts" :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
import resize from './mixins/resize'

const animationDuration = 2000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    rows: {
      type: Object,
      default: () => {},
      required: true
    },
    name: {
      type: String,
      default: 'pageA',
      required: true
    },
    title: {
      type: String,
      default: ''
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    }
  },
  data() {
    return {
      chart: null,
      timer: null,
      status: true,
      startValue: 1,
      endValue: 5
    }
  },
  watch: {
    rows: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    clearInterval(this.timer)
    this.timer = null
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      if (this.rows.urls) {
        this.setOptions(this.rows)
      }
    },
    setOptions({ urls, counts, times }) {
      const data = counts || times
      const option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        title: {
          text: this.title
        },
        grid: {
          top: '12%',
          left: '2%',
          right: '2%',
          bottom: '15%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: urls,
          axisLabel: {
            show: false
          },
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: [{
          type: 'value',
          minInterval: 1,
          axisTick: {
            show: false
          }
        }],
        dataZoom: [
          {
            type: 'slider',
            zoomLock: true,
            startValue: 0,
            endValue: 4,
            brushSelect: false
          }
        ],
        series: [{
          name: this.name,
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data,
          animationDuration
        }]
      }
      // 防止页面白屏默认加载一次
      this.chart.setOption(option)
      this.getZoom(data)
      // 自动滚动
      this.timer = setInterval(() => {
        if (this.endValue >= data.length - 1) {
          this.status = false
          this.endValue = data.length - 1
          this.startValue = this.endValue - 4
        } else if (this.startValue <= 0) {
          this.status = true
          this.startValue = 0
          this.endValue = this.startValue + 4
        }
        option.dataZoom[0].endValue = this.status ? this.endValue++ : this.endValue--
        option.dataZoom[0].startValue = this.status ? this.startValue++ : this.startValue--
        this.chart.setOption(option)
      }, 2000)
    },
    // 手动滚动监听
    getZoom(data) {
      this.chart.on('dataZoom', params => {
        this.endValue = (params.end / 100) * data.length
        this.startValue = this.endValue - 4
      })
    }
  }
}
</script>
